import {
  createStore,
  applyMiddleware,
  combineReducers
} from 'redux';

import {
  PersistConfig,
  persistReducer
} from 'redux-persist';

import thunk from 'redux-thunk';

import config from './../../config';

import getReducers from './reducers';

import { ModelOptions } from './types';

import localStorage from 'redux-persist/lib/storage';

const createStoreAsync = applyMiddleware(thunk)(createStore);

const persistOptions: PersistConfig<any> = {
  blacklist: [],
  debug: __DEV__,
  whitelist: ['user'],
  storage: localStorage,
  key: `builder_${config.version}`
};

export default (opts?: ModelOptions) => {
 return createStoreAsync(createPersistReducer(opts));
};

export const createPersistReducer = <State>(opts?: ModelOptions) => {
  return persistReducer(persistOptions, combineReducers(getReducers<State>(opts)));
};




